package com.smilive.start.netty.rpc;

import java.util.HashMap;

/**
 * @program: netty
 * @Date: 2022-01-11 10:55
 * @Author: smilive
 * @Description:
 */
public class ServicesFactory {
    static HashMap<Class<?>, Object> map = new HashMap<>(16);

    public static Object getInstance(Class<?> interfaceClass) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        // 根据Class创建实例
        try {
            Class<?> clazz = Class.forName("com.smilive.start.netty.rpc.server.RpcHelloServer");
            Object instance = Class.forName("com.smilive.start.netty.rpc.server.impl.RpcHelloServerImpl").newInstance();

            // 放入 InterfaceClass -> InstanceObject 的映射
            map.put(clazz, instance);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return map.get(interfaceClass);
    }
}
